/*
 * Copyright 2017 the original author or authors.
 *
 */

package com.ivo.util;

import java.text.ParseException;
import java.util.Date;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;

/**
 * <h1>title</h1>
 * <p>
 * content
 * </p>
 * <b>Note:</b>
 *
 * @author nexf
 * @since 2017/4/2
 */
public class DateUtil {

  public static final String DATA_PATTERN = "yyyy-MM-dd";

  public static final String DATA_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";

  public static final Date FORVER = DateUtil.parseDate("9999-12-31");

  public static Date getNow() {
    return new Date();
  }

  public static Date parseDate(String date) {
    return DateUtil.parse(date, DATA_PATTERN);
  }

  public static Date parseDateTime(String date) {
    return DateUtil.parse(date, DATA_TIME_PATTERN);
  }

  public static Date parse(String date, String pattern) {
    if (date == null) {
      return null;
    }
    try {
      return DateUtils.parseDate(date, pattern);
    } catch (ParseException e) {
      throw ExceptionUtil.unchecked(e);
    }
  }

  public static String formatDate(Date date) {
    return format(date, DATA_PATTERN);
  }

  public static String formatDateTime(Date date) {
    return format(date, DATA_TIME_PATTERN);
  }

  public static String format(Date date, String pattern) {
    if (date == null) {
      return null;
    }
    return DateFormatUtils.format(date, pattern);
  }

  public static Date addMilliseconds(Date date, int amount) {
    if (date == null) {
      return null;
    }
    return DateUtils.addMilliseconds(date, amount);
  }

  public static Date addSeconds(Date date, int amount) {
    if (date == null) {
      return null;
    }
    return DateUtils.addSeconds(date, amount);
  }

  public static Date addMinutes(Date date, int amount) {
    if (date == null) {
      return null;
    }
    return DateUtils.addMinutes(date, amount);
  }

  public static Date addHours(Date date, int amount) {
    if (date == null) {
      return null;
    }
    return DateUtils.addHours(date, amount);
  }

  public static Date addDays(Date date, int amount) {
    if (date == null) {
      return null;
    }
    return DateUtils.addDays(date, amount);
  }

  public static Date addWeeks(Date date, int amount) {
    if (date == null) {
      return null;
    }
    return DateUtils.addWeeks(date, amount);
  }

  public static Date addMonths(Date date, int amount) {
    if (date == null) {
      return null;
    }
    return DateUtils.addMonths(date, amount);
  }

  public static Date addYears(Date date, int amount) {
    if (date == null) {
      return null;
    }
    return DateUtils.addYears(date, amount);
  }

  /**
   * @param date 待取整对象
   * @param field {@code Calendar} or <code>SEMI_MONTH</code>
   * @return 结果
   */
  public static Date ceiling(Date date, int field) {
    if (date == null) {
      return null;
    }
    return DateUtils.ceiling(date, field);
  }

}
